<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('添加成员')" />
    <th:block th:include="include :: select2-css" />
</head>
<body>
<div class="main-content">
    <form id="form-user-add" class="form-horizontal">
        <input type="hidden" id="prjgroupId" name="prjgroupId" th:value="${prjgroup.prjgroupId}">
        <h4 class="form-header h4">基本信息</h4>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">项目组名称：</label>
                    <div class="col-sm-8">
                        <input name="userName" class="form-control" type="text" disabled th:value="${prjgroup.prjgroupName}">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">负责人：</label>
                    <div class="col-sm-8">
                        <input name="loginName" class="form-control" type="text" disabled th:value="${prjgroup.leader}">
                    </div>
                </div>
            </div>


        </div>

        <h4 class="form-header h4">添加成员</h4>
        <div class="col-sm-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">归属部门：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="deptId" type="hidden" id="treeId" hidden/>
                        <input name="deptName"onclick="selectDeptTree()" id="treeName" type="text" placeholder="按部门查询添加" class="form-control" >
                        <span class="input-group-addon" onclick="chaxun()"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: select2-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/prjgroup";
    var employees = [[${employees}]]
    //crazy(employees);
    function chaxun(){

        var prjgroupId=document.getElementById("prjgroupId").value
        var deptId=document.getElementById("treeId").value
        // console.log("选中"+deptId+prjgroupId)
        // var url = prefix + '/aaddMember/' + prjgroupId+'/'+deptId;
        // $.modal.reload("添加成员", url);


        $.ajax({
            url: prefix + '/aaddMember/' + prjgroupId+'/'+deptId,
            Type:"get",
            success:function(res){
                employees=res;
                // console.log(employees);
                //重新加载table
                crazy(employees);
            },
            error:function(err){
                $.modal.alert(err+"",modal_status.FAIL)
            }
        })

    }
function crazy(employees){
    $(function() {
        var options = {
            data: employees,
            sidePagination: "client",
            sortName: "roleSort",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            clickToSelect: true,//是否启用点击选中行
            maintainSelected: false,
            columns: [{
                checkbox: true,
                formatter:function (value, row, index) {
                    if($.common.isEmpty(value)) {
                        // console.log(row);
                        return { checked: row.flag };
                    } else {
                        return { checked: value }
                    }
                }
            },
                {
                    field: 'employeeId',
                    title: '员工ID'
                },
                {
                    field: 'name',
                    title: '员工姓名',
                    sortable: true
                },
                {
                    field: 'empNo',
                    title: '员工编号'
                },
                {
                    field: 'deptName',
                    title: '部门'
                },
                {
                    field: 'identiy',
                    title: '身份',
                    visible: false,
                    formatter: function (value,row,index) {
                        if(value==1){
                            return "部门领导";
                        }else if(value==2){
                            return "项目组长";
                        }else{
                            return "普通员工";
                        }
                    }
                },
                {
                    field: 'gender',
                    title: '性别',
                    visible: false,
                    formatter:function(value){
                        if(value == 1){
                            return "男";
                        }else{
                            return "女";
                        }
                    }
                },
                {
                    field: 'mobile',
                    title: '手机',
                    visible: false
                },
                {
                    title: '员工状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        // console.log(value)
                        // console.log(row)
                        return statusFormater(row.status);
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                }]
        };
        /*初始化表格参数*/
        $.table.init(options);
    });

}

    /* 添加角色-提交 */
    function submitHandler(index, layero){
        var rows = $.table.selectFirstColumns();
        var data = { "prjgroupId": $("#prjgroupId").val(), "employeeIds": rows.join() };
        $.operate.save(prefix + "/insertMember", data);
    }
    /* 用户管理-新增-选择部门树 */
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "1" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        // console.log(1)
        $.modal.openOptions(options);
    }
    function doSubmit(index, layero){
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }
    function statusFormater(status){
        var statusArr = [[${@dict.getType('sys_employee_status')}]];
        for(var i=0;i<statusArr.length;i++){
            if (statusArr[i].dictValue==status){
                return statusArr[i].dictLabel;
            }
        }
    }
</script>
</body>
</html>